package com.ruoyi.warehouse.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sfproduct.domain.LwSfproduct;
import com.ruoyi.sfproduct.mapper.LwSfproductMapper;
import com.ruoyi.warehouse.domain.LwFpWarehouseWl;
import com.ruoyi.warehouse.domain.LwSfpWarehouse;
import com.ruoyi.warehouse.domain.LwWarehousinginfo;
import com.ruoyi.warehouse.mapper.LwFpWarehouseWlMapper;
import com.ruoyi.warehouse.mapper.LwWarehousinginfoMapper;
import com.ruoyi.warehouse.service.ILwFpWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.warehouse.mapper.LwFpWarehouseMapper;
import com.ruoyi.warehouse.domain.LwFpWarehouse;

/**
 * 成品仓库管理Service业务层处理
 * 
 * @author sll
 * @date 2023-09-19
 */
@Service
public class LwFpWarehouseServiceImpl implements ILwFpWarehouseService
{
    @Autowired
    private LwFpWarehouseMapper lwFpWarehouseMapper;

    @Autowired
    private LwFpWarehouseWlMapper lwFpWarehouseWlMapper;

    @Autowired
    private LwWarehousinginfoMapper LwWarehousinginfoMapper;

    @Autowired
    private LwSfproductMapper lwSfproductMapper;

    /**
     * 查询成品仓库管理
     * 
     * @param id 成品仓库管理主键
     * @return 成品仓库管理
     */
    @Override
    public LwFpWarehouse selectLwFpWarehouseById(Long id)
    {
        return lwFpWarehouseMapper.selectLwFpWarehouseById(id);
    }

    /**
     * 查询成品仓库管理列表
     * 
     * @param lwFpWarehouse 成品仓库管理
     * @return 成品仓库管理
     */
    @Override
    public List<LwFpWarehouse> selectLwFpWarehouseList(LwFpWarehouse lwFpWarehouse)
    {
        return lwFpWarehouseMapper.selectLwFpWarehouseList(lwFpWarehouse);
    }

    /**
     * 新增成品仓库管理
     * 
     * @param lwFpWarehouse 成品仓库管理
     * @return 结果
     */
    @Override
    public int insertLwFpWarehouse(LwFpWarehouse lwFpWarehouse)
    {
        if (lwFpWarehouse.getQuantity() != 0){
        // 添加成品入库信息记录
        LwWarehousinginfo warehousinginfo = new LwWarehousinginfo();
        warehousinginfo.setCourseId(lwFpWarehouse.getCourseId());
        warehousinginfo.setSfproductName(lwFpWarehouse.getSfproductName());
        warehousinginfo.setModel(lwSfproductMapper.selectLwSfproductBySfproductId(lwFpWarehouse.getSfproductId()).getModel());
        warehousinginfo.setParameter(lwSfproductMapper.selectLwSfproductBySfproductId(lwFpWarehouse.getSfproductId()).getParameter());
        warehousinginfo.setTime(new Date());
        warehousinginfo.setEmployeeId(SecurityUtils.getUserId());
        warehousinginfo.setFlow(1L);
        warehousinginfo.setCompleted(lwFpWarehouse.getQuantity());
        warehousinginfo.setRemark(lwFpWarehouse.getRemark());
        lwFpWarehouseMapper.insertWarehousinginfo(warehousinginfo);
        }

        LwFpWarehouse isEmpty = lwFpWarehouseMapper.selectLwFpWarehouseBySfproductId(lwFpWarehouse.getSfproductId());
        if (isEmpty == null){
            lwFpWarehouse.setRemark(null);

            LwSfproduct lwFproduct = lwSfproductMapper.selectLwSfproductBySfproductId(lwFpWarehouse.getSfproductId());
            lwFpWarehouse.setSfproductName(lwFproduct.getSfproductName());
            lwFpWarehouse.setModel(lwFproduct.getModel());
            lwFpWarehouse.setParameter(lwFproduct.getParameter());

            return lwFpWarehouseMapper.insertLwFpWarehouse(lwFpWarehouse);
        }else if (isEmpty.getSfproductId().equals(lwFpWarehouse.getSfproductId())&& !Objects.equals(isEmpty.getIfSelfProduced(), lwFpWarehouse.getIfSelfProduced())) {
            LwSfproduct lwFproduct = lwSfproductMapper.selectLwSfproductBySfproductId(lwFpWarehouse.getSfproductId());
            lwFpWarehouse.setSfproductName(lwFproduct.getSfproductName());
            lwFpWarehouse.setModel(lwFproduct.getModel());
            lwFpWarehouse.setParameter(lwFproduct.getParameter());
            return lwFpWarehouseMapper.insertLwFpWarehouse(lwFpWarehouse);
        }else{

            Long quantity = isEmpty.getQuantity() + lwFpWarehouse.getQuantity();
            lwFpWarehouse.setQuantity(quantity);
            lwFpWarehouse.setId(isEmpty.getId());
            lwFpWarehouse.setRemark(isEmpty.getRemark());
            return lwFpWarehouseMapper.updateLwFpWarehouse(lwFpWarehouse);
        }

    }

    /**
     * 修改成品仓库管理
     * 
     * @param lwFpWarehouse 成品仓库管理
     * @return 结果
     */
    @Override
    public int updateLwFpWarehouse(LwFpWarehouse lwFpWarehouse)
    {
        return lwFpWarehouseMapper.updateLwFpWarehouse(lwFpWarehouse);
    }

    /**
     * 批量删除成品仓库管理
     * 
     * @param ids 需要删除的成品仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteLwFpWarehouseByIds(Long[] ids)
    {
        return lwFpWarehouseMapper.deleteLwFpWarehouseByIds(ids);
    }

    /**
     * 删除成品仓库管理信息
     * 
     * @param id 成品仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteLwFpWarehouseById(Long id)
    {
        return lwFpWarehouseMapper.deleteLwFpWarehouseById(id);
    }

    @Override
    public int updateLwFpWarehouseQuantity(LwFpWarehouse lwFpWarehouse) {
        if (lwFpWarehouse.getVariable() < 0){
            return -1;
        }
        if (lwFpWarehouse.getRemove() > lwFpWarehouse.getQuantity()){
            return -2;
        }
        if (lwFpWarehouse.getRemove() < 0){
            return -1;
        }
        // 新建仓库记录对象
        LwWarehousinginfo warehousingInfo = new LwWarehousinginfo();
        warehousingInfo.setSfproductName(lwFpWarehouse.getSfproductName());
        warehousingInfo.setModel(lwFpWarehouse.getModel());
        warehousingInfo.setParameter(lwFpWarehouse.getParameter());
        warehousingInfo.setTime(new Date());
        warehousingInfo.setEmployeeId(SecurityUtils.getUserId());
        warehousingInfo.setRemark(lwFpWarehouse.getRemark());
        // 入库
        if (lwFpWarehouse.getVariable() > 0) {
            lwFpWarehouse.setQuantity(lwFpWarehouse.getQuantity() + lwFpWarehouse.getVariable());
            warehousingInfo.setFlow(1L);
            warehousingInfo.setCompleted(lwFpWarehouse.getVariable());
        }
        //出库
        if (lwFpWarehouse.getRemove() > 0){
            lwFpWarehouse.setQuantity(lwFpWarehouse.getQuantity() - lwFpWarehouse.getRemove());
            //outboundType 0正常出库 1样品出库 2换货出库
            String outboundType = lwFpWarehouse.getOutboundType();
            switch (outboundType) {
                // 3L正常出库 11L样品出库 12L换货出库
                case "0": warehousingInfo.setFlow(3L); // 正常出库
                    break;
                case "1": warehousingInfo.setFlow(11L); // 样品出库
                    break;
                case "2": warehousingInfo.setFlow(12L); // 换货出库
                    break;
                default:
                    // 处理未知的outboundType
                    break;
            }
            warehousingInfo.setCompleted(lwFpWarehouse.getRemove());

            warehousingInfo.setClient(lwFpWarehouse.getClient());
            warehousingInfo.setSalesman(lwFpWarehouse.getSalesman());
        }
        LwWarehousinginfoMapper.insertLwWarehousinginfo(warehousingInfo);

        LwFpWarehouse fpWarehouse = lwFpWarehouseMapper.selectLwFpWarehouseById(lwFpWarehouse.getId());
        lwFpWarehouse.setRemark(fpWarehouse.getRemark());
        return lwFpWarehouseMapper.updateLwFpWarehouse(lwFpWarehouse);
    }

    @Override
    public int transferInventory(LwFpWarehouse lwFpWarehouse) {
        // 条件筛选
        if (lwFpWarehouse.getVariable() < 0){
            return -1;
        }
        if (lwFpWarehouse.getRemove() > lwFpWarehouse.getQuantity()){
            return -2;
        }
        if (lwFpWarehouse.getRemove() < 0){
            return -3;
        }

        //查询网联库里是否存在
        LwFpWarehouseWl lwFpWarehouseWl = lwFpWarehouseWlMapper.selectLwSfproductBySfproductId(lwFpWarehouse.getSfproductId());

        // 不存在就新建兵添加接收到的所有字段赋值，并对成品库数量进行相应删减，保存记录信息
        if (lwFpWarehouseWl == null){
            lwFpWarehouseWl = new LwFpWarehouseWl();
            lwFpWarehouseWl.setSfproductId(lwFpWarehouse.getSfproductId());
            lwFpWarehouseWl.setSfproductName(lwFpWarehouse.getSfproductName());
            lwFpWarehouseWl.setModel(lwFpWarehouse.getModel());
            lwFpWarehouseWl.setParameter(lwFpWarehouse.getParameter());
            lwFpWarehouseWl.setIfSelfProduced(lwFpWarehouse.getIfSelfProduced());
            lwFpWarehouseWl.setQuantity(lwFpWarehouse.getRemove());

            //保存记录信息
            saveInfo(lwFpWarehouseWl,lwFpWarehouse);

            lwFpWarehouse.setQuantity(lwFpWarehouse.getQuantity() - lwFpWarehouse.getRemove());
            LwFpWarehouse fpWarehouse = lwFpWarehouseMapper.selectLwFpWarehouseById(lwFpWarehouse.getId());
            lwFpWarehouse.setRemark(fpWarehouse.getRemark());
            lwFpWarehouseMapper.updateLwFpWarehouse(lwFpWarehouse);

            return lwFpWarehouseWlMapper.insertLwFpWarehouseWl(lwFpWarehouseWl);
        }
        // 如果存在，直接赋值，并修改成品库相应数量，保存记录信息
        else {
            saveInfo(lwFpWarehouseWl,lwFpWarehouse);

            lwFpWarehouse.setQuantity(lwFpWarehouse.getQuantity() - lwFpWarehouse.getRemove());
            LwFpWarehouse fpWarehouse = lwFpWarehouseMapper.selectLwFpWarehouseById(lwFpWarehouse.getId());
            lwFpWarehouse.setRemark(fpWarehouse.getRemark());
            lwFpWarehouseMapper.updateLwFpWarehouse(lwFpWarehouse);

            lwFpWarehouseWl.setQuantity(lwFpWarehouseWl.getQuantity() + lwFpWarehouse.getRemove());

            return lwFpWarehouseWlMapper.updateLwFpWarehouseWl(lwFpWarehouseWl);
        }
    }

    // 保存记录
    private void saveInfo(LwFpWarehouseWl lwFpWarehouseWl,LwFpWarehouse lwFpWarehouse){
        // 新建仓库记录对象
        LwWarehousinginfo warehousingInfo = new LwWarehousinginfo();
        warehousingInfo.setSfproductName(lwFpWarehouseWl.getSfproductName());
        warehousingInfo.setModel(lwFpWarehouseWl.getModel());
        warehousingInfo.setParameter(lwFpWarehouseWl.getParameter());
        warehousingInfo.setTime(new Date());
        warehousingInfo.setEmployeeId(SecurityUtils.getUserId());
        warehousingInfo.setRemark(lwFpWarehouse.getRemark());
        warehousingInfo.setFlow(6L);
        warehousingInfo.setCompleted(lwFpWarehouse.getRemove());
        LwWarehousinginfoMapper.insertLwWarehousinginfo(warehousingInfo);
    }

}
